/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.veditor;

import java.net.URL;
import java.util.StringTokenizer;
import offset.nodes.client.editor.Editor;
import offset.nodes.client.model.ServerModel;

/**
 *
 * @author Walter Lütgenau
 */
public class VirtualEditorApplet extends VirtualEditor {
    public static final String PAR_DOWNLOAD_FROM = "downloadFrom";
    public static final String PAR_UPLOAD_TO = "uploadTo";
    public static final String PAR_RETURN_URL = "returnUrl";
    public static final String PAR_MODE = "mode";
    public static final String PAR_INSTANCE_PATH = "instancePath";
    public static final String PAR_PARENT_PATH = "parentPath"; 
    
    /** Creates a new instance of EditorApplet */
    public VirtualEditorApplet() {
    }

    public void startUI() {
        try {
            super.startUI();

            String downloadFrom = (String) getParameter(PAR_DOWNLOAD_FROM);
            String uploadTo = getParameter(PAR_UPLOAD_TO);
            String instancePath = getParameter(PAR_INSTANCE_PATH);
            String returnUrl = getParameter(PAR_RETURN_URL);
            String parentPath = getParameter(PAR_PARENT_PATH);
            String download = downloadFrom.replaceAll(" ", "%20");
            URL downloadURL = new URL(download);
            URL documentURL = new URL(downloadFrom);
            String downloadPath = documentURL.getPath();
            StringBuffer documentPath = new StringBuffer();
            StringTokenizer parser = new StringTokenizer(downloadPath, "/");
            int i = 0;
            while (parser.hasMoreTokens()) {
                String pathComponent = parser.nextToken();
                if (i >= 2)
                    documentPath.append("/" + pathComponent);
                i++;
            }
            getProperties().put(PROP_DOCUMENT_PATH, documentPath.toString());
            getProperties().put(PROP_DOCUMENT_URL, downloadFrom);
            getProperties().put(PROP_UPLOAD_TO, uploadTo);
            getProperties().put(PROP_INSTANCE_PATH, instancePath);
            getProperties().put(PROP_PARENT_PATH, parentPath);
            getProperties().put(Editor.PROP_NODE_TYPES, getNodeTypes(new ServerModel(new URL(uploadTo))));            
            if (returnUrl != null && returnUrl.length() > 0)
                getProperties().put(PROP_RETURN_URL, returnUrl);

            String modeString = getParameter(PAR_MODE);
            if (modeString != null && modeString.length() > 0) {
                int mode = Integer.parseInt(modeString);
                setMode(mode);
                setPage(downloadURL);
                setupPane();
            }

            getBrowserPane().requestFocusInWindow();
            setVisible(true);

        } catch (Exception e) {
            e.printStackTrace(); // for java console window
            throw new RuntimeException(e);
        }
    }
    
}
